package hot100.logos11To20;

import java.util.Arrays;

/**
 * @author logos
 * date 2025/7/12 10:16
 * @version v1.0
 * @Package hot100
 */
public class logos18 {

    /**
     * 18.矩阵置零
     * https://leetcode.cn/problems/set-matrix-zeroes/?envType=study-plan-v2&envId=top-100-liked
     */

    class Solution {
        public void setZeroes(int[][] matrix) {
            int n = matrix.length;
            int m = matrix[0].length;
            boolean row = false;
            boolean col = false;
            for (int i = 0; i < n; i++) {
                if (matrix[i][0] == 0) {
                    col = true;
                    break;
                }
            }
            for (int j = 0; j < m; j++) {
                if (matrix[0][j] == 0) {
                    row = true;
                    break;
                }
            }
            for (int i = 1; i < n; i++) {
                for (int j = 1; j < m; j++) {
                    if (matrix[i][j] == 0) {
                        matrix[i][0] = matrix[0][j] = 0;
                    }
                }
            }
            for (int i = 1; i < n; i++) {
                if (matrix[i][0] == 0) {
                    Arrays.fill(matrix[i], 0);
                }
            }
            for (int j = 1; j < m; j++) {
                if (matrix[0][j] == 0) {
                    for (int i = 1; i < n; i++) {
                        matrix[i][j] = 0;
                    }
                }
            }
            if (row) {
                Arrays.fill(matrix[0], 0);
            }
            if (col) {
                for (int i = 0; i < n; i++) {
                    matrix[i][0] = 0;
                }
            }
        }
    }
}
